package com.example.tao_city.myapplication;




import android.content.DialogInterface;
import android.graphics.drawable.BitmapDrawable;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.Filter;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

import static android.widget.Toast.LENGTH_SHORT;



public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private ListView listView1;
    private PopupWindow mtext1;
    private PopupWindow mtext2;
    private PopupWindow mtext3;
    private static Boolean isExit = false;
    private TextView tv1;
    private TextView tv2;
    private TextView tv3;
    private Button teb1,teb2,teb3,teb4,teb5,teb6,teb7,teb8,teb9,teb10,teb11;
    private Button wd1,wd2,wd3,wd4;
    private Button sk1,sk2,sk3,sk4,sk5;
    private Button back;
    private String zhandian=null,leix=null,yewu=null;
    private List<Map<String,String>> list=new ArrayList<Map<String,String>>();
    private List<Map<String,String>> list1=new ArrayList<Map<String,String>>();
    private SimpleAdapter simpleAdapter;
    private static final String[] form=new String[]{"zd","lx","dz"};
    private static final int[] to={R.id.te1,R.id.tb1,R.id.tb3};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        instiation();
        onclick();
        list();
        listView1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                String m=list.get(i).get("zd");
                String n=listView1.getItemAtPosition(i)+"";
                Toast.makeText(MainActivity.this,n, Toast.LENGTH_SHORT).show();
            }
        });

        /*listView1.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                if (motionEvent.getAction()==MotionEvent.ACTION_MOVE){
                    Toast.makeText(MainActivity.this,"你移动了列表",Toast.LENGTH_SHORT).show();
                }
                if (motionEvent.getAction()==MotionEvent.ACTION_UP){
                    Toast.makeText(MainActivity.this,"你离开了列表",Toast.LENGTH_SHORT).show();
                }
                if (motionEvent.getAction()==MotionEvent.ACTION_DOWN){
                    Toast.makeText(MainActivity.this,"你触摸了列表",Toast.LENGTH_SHORT).show();
                }
                    return false;
            }
        });
        //有问题，移动之后一直Toast你移动了列表*/
    }

   /*
   调用显示menu
   @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main, menu);
        return true;
    }*/
    /*
    实例化按钮
    */

    private void instiation(){
        back=(Button)findViewById(R.id.back);
        listView1=(ListView)findViewById(R.id.listview1);
        tv2=(TextView) findViewById(R.id.btn2);
        tv1=(TextView) findViewById(R.id.btn1);
        tv3=(TextView) findViewById(R.id.btn3);
    }
    /*
    监听事件函数
     */
    private void onclick(){
        tv1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                show1();
            }
        });
        tv2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                show2();
            }
        });
        tv3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                show3();
            }
        });
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               // Toast.makeText(MainActivity.this,"你点击了返回",Toast.LENGTH_SHORT).show();
                new AlertDialog.Builder(MainActivity.this).setTitle("系统提示!")
                        .setMessage("确定要退出吗？")
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                finish();
                            }
                        }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                }).show();
            }
        });
    }
    public boolean onKeyDown(int keyCode, KeyEvent event){
        if(keyCode == KeyEvent.KEYCODE_BACK)
        {
            Timer tExit = null;
            if (isExit == false) {
                isExit = true; // 准备退出
                Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
                tExit = new Timer();
                tExit.schedule(new TimerTask() {
                    @Override
                    public void run() {
                        isExit = false; // 取消退出
                    }
                }, 2000); // 如果2秒钟内没有按下返回键，则启动定时器取消掉刚才执行的任务

            } else {
                finish();
                System.exit(0);
            }
        }
        return false;
    }
    public void onClick(View v){
        int id=v.getId();
        switch (id){
            case R.id.teb1:
                tv1.setText(teb1.getText());
                listView1.setBackgroundResource(0);
                tv1.setText(R.string.ac1);
                tv2.setText(R.string.ac2);
                tv3.setText(R.string.ac3);
                list.clear();
                list();
                zhandian=null;
                yewu=null;
                mtext1.dismiss();
                break;
            case R.id.teb2:
                tv1.setText(teb2.getText());
                dianjiqiehuang(teb2);
                /*listView1.setTextFilterEnabled(true);
                listView1.setFilterText((String) teb2.getText());
                /*CharSequence cha= teb2.getText();
                Filter file=simpleAdapter.getFilter();
                file.filter(cha);
                String a=list.size()+"";
                Toast.makeText(this,a,Toast.LENGTH_LONG).show();
                /*for(int i=0;i<a-1;i++){
                if(list.get(i).get("zd").equals(list.get(i+1).get("zd"))){
                        list.remove(i);
                    }
                }*/
                simpleAdapter.notifyDataSetChanged();
                kong();
                mtext1.dismiss();
                break;
            case R.id.teb3:
                tv1.setText(teb3.getText());
                dianjiqiehuang(teb3);
                kong();
                mtext1.dismiss();
                break;
            case R.id.teb4:
                tv1.setText(teb4.getText());
                dianjiqiehuang(teb4);
                kong();
                mtext1.dismiss();
                break;
            case R.id.teb5:
                tv1.setText(teb5.getText());
                dianjiqiehuang(teb5);
                kong();
                mtext1.dismiss();
                break;
            case R.id.teb6:
                tv1.setText(teb6.getText());
                dianjiqiehuang(teb6);
                kong();
                mtext1.dismiss();
                break;
            case R.id.teb7:
                tv1.setText(teb7.getText());
                dianjiqiehuang(teb7);
                kong();
                mtext1.dismiss();
                break;
            case R.id.teb8:
                tv1.setText(teb8.getText());
                dianjiqiehuang(teb8);
                kong();
                mtext1.dismiss();
                break;
            case R.id.teb9:
                tv1.setText(teb9.getText());
                dianjiqiehuang(teb9);
                kong();
                mtext1.dismiss();
                break;
            case R.id.teb10:
                tv1.setText(teb10.getText());
                dianjiqiehuang(teb10);
                kong();
                mtext1.dismiss();
                break;
            case R.id.teb11:
                tv1.setText(teb11.getText());
                dianjiqiehuang(teb11);
                kong();
                mtext1.dismiss();
                break;
            case  R.id.wd1:
                tv2.setText(wd1.getText());
                listView1.setBackgroundResource(0);
                tv1.setText(R.string.ac1);
                tv2.setText(R.string.ac2);
                tv3.setText(R.string.ac3);
                list.clear();
                list();
                zhandian=null;
                yewu=null;
                mtext2.dismiss();
                break;
            case  R.id.wd2:
                tv2.setText(wd2.getText());
                mtext2.dismiss();
                break;
            case  R.id.wd3:
                tv2.setText(wd3.getText());
                mtext2.dismiss();
                break;
            case  R.id.wd4:
                tv2.setText(wd4.getText());
                mtext2.dismiss();
                break;
            case R.id.sk1:
                tv3.setText(sk1.getText());
                yewu=null;
                break;
            case R.id.sk2:
                yewu= (String) sk2.getText();
                tv3.setText(sk2.getText());
                break;
            case R.id.sk3:
                yewu= (String) sk3.getText();
                tv3.setText(sk3.getText());
                break;
            case R.id.sk4:
                tv1.setText(R.string.ac1);
                tv2.setText(R.string.ac2);
                tv3.setText(R.string.ac3);
                yewu=null;
                zhandian=null;
                listView1.setBackgroundResource(0);
                list.clear();
                list();
                mtext3.dismiss();
                break;
            case R.id.sk5:
                yewuxuanze();
                kong();
                if(list.size()!=0) {
                    Toast.makeText(this, "您选择了：" + tv1.getText() + "," + tv2.getText() + "," + tv3.getText(), LENGTH_SHORT).show();
                }
                mtext3.dismiss();
                break;

        }
    }


    /*
    设置点击展开的配置
     */
    private void show1() {

            View contentview1 = LayoutInflater.from(MainActivity.this).inflate(R.layout.text1, null);//设置要显示的页面
            mtext1 = new PopupWindow(contentview1);
            mtext1.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
            mtext1.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);//设置宽高
            mtext1.setFocusable(true);//设置为true表示可以聚焦
            mtext1.setBackgroundDrawable(new BitmapDrawable(getResources()));//设置背景，用物理键返回的时候
            contentview1.setOnTouchListener(new View.OnTouchListener() {//设置点击后取消，即使点击外部也可以捕获
                @Override
                public boolean onTouch(View view, MotionEvent motionEvent) {
                    if (mtext1.isShowing()) {
                        mtext1.dismiss();
                    }
                    return false;
                }
            });
        mtext1.setOnDismissListener(new PopupWindow.OnDismissListener() {//设置当窗口消失时执行的命令
            @Override
            public void onDismiss() {
                tv1.setBackgroundResource(R.mipmap.two_1);
            }
        });
        if (mtext1.isShowing()){
            mtext1.dismiss();
        } else {
            tv1.setBackgroundResource(R.mipmap.two_3);
            mtext1.showAsDropDown(tv1);//显示在tv1下面
        }
        //设置展开的内容里的按钮监听
        teb1 = (Button) contentview1.findViewById(R.id.teb1);
        teb2 = (Button) contentview1.findViewById(R.id.teb2);
        teb3 = (Button) contentview1.findViewById(R.id.teb3);
        teb4 = (Button) contentview1.findViewById(R.id.teb4);
        teb5 = (Button) contentview1.findViewById(R.id.teb5);
        teb6 = (Button) contentview1.findViewById(R.id.teb6);
        teb7 = (Button) contentview1.findViewById(R.id.teb7);
        teb8 = (Button) contentview1.findViewById(R.id.teb8);
        teb9 = (Button) contentview1.findViewById(R.id.teb9);
        teb10 = (Button) contentview1.findViewById(R.id.teb10);
        teb11= (Button) contentview1.findViewById(R.id.teb11);
        teb1.setOnClickListener(this);
        teb2.setOnClickListener(this);
        teb3.setOnClickListener(this);
        teb4.setOnClickListener(this);
        teb5.setOnClickListener(this);
        teb6.setOnClickListener(this);
        teb7.setOnClickListener(this);
        teb8.setOnClickListener(this);
        teb9.setOnClickListener(this);
        teb10.setOnClickListener(this);
        teb11.setOnClickListener(this);

    }
    private void show2(){
            View contentivew2 = LayoutInflater.from(MainActivity.this).inflate(R.layout.text2, null);
            mtext2 = new PopupWindow(contentivew2);
            mtext2.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
            mtext2.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
            mtext2.setFocusable(true);
            mtext2.setBackgroundDrawable(new BitmapDrawable(getResources()));
            contentivew2.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View view, MotionEvent motionEvent) {
                    if (mtext2.isShowing()) {
                        mtext2.dismiss();
                    }
                    return false;
                }
            });
        mtext2.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                tv2.setBackgroundResource(R.mipmap.two_1);
            }
        });
        if (mtext2.isShowing()) {
            mtext2.dismiss();
        }else {
            mtext2.showAsDropDown(tv2);
            tv2.setBackgroundResource(R.mipmap.two_3);
        }
        wd1=(Button)contentivew2.findViewById(R.id.wd1);
        wd2=(Button)contentivew2.findViewById(R.id.wd2);
        wd3=(Button)contentivew2.findViewById(R.id.wd3);
        wd4=(Button)contentivew2.findViewById(R.id.wd4);
        wd1.setOnClickListener(this);
        wd2.setOnClickListener(this);
        wd3.setOnClickListener(this);
        wd4.setOnClickListener(this);
    }
    private void show3(){
            View contentivew3 = LayoutInflater.from(MainActivity.this).inflate(R.layout.text3, null);
            mtext3 = new PopupWindow(contentivew3);
            mtext3.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
            mtext3.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
            mtext3.setFocusable(true);
            mtext3.setBackgroundDrawable(new BitmapDrawable(getResources()));
            contentivew3.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View view, MotionEvent motionEvent) {
                    if (mtext3.isShowing()) {
                        mtext3.dismiss();
                    }
                    return false;
                }
            });
        mtext3.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                tv3.setBackgroundResource(R.mipmap.two_1);
            }
        });
        if (mtext3.isShowing()) {
            mtext3.dismiss();
        }else {
            mtext3.showAsDropDown(tv3);
            tv3.setBackgroundResource(R.mipmap.two_3);
        }
        sk1=(Button)contentivew3.findViewById(R.id.sk1);
        sk2=(Button)contentivew3.findViewById(R.id.sk2);
        sk3=(Button)contentivew3.findViewById(R.id.sk3);
        sk4=(Button)contentivew3.findViewById(R.id.sk4);
        sk5=(Button)contentivew3.findViewById(R.id.sk5);
        sk1.setOnClickListener(this);
        sk2.setOnClickListener(this);
        sk3.setOnClickListener(this);
        sk4.setOnClickListener(this);
        sk5.setOnClickListener(this);
    }
    /*
    添加数据
    设置列表适配器
     */
    private void list(){
        //手写添加数据
        Map<String,String> map = new HashMap<String, String>();
        map.put("zd", "唐家、金鼎");
        map.put("lx", "售卡，充值");
        map.put("dz", "香洲区唐家湾镇金凤路K3和10A等公交车总站");
        list.add(map);

        Map<String,String> map1=new HashMap<String, String>();
        map1.put("zd", "香洲");
        map1.put("lx", "售卡");
        map1.put("dz", "香洲区紫荆路142号");
        list.add(map1);

        Map<String,String> map2=new HashMap<String, String>();
        map2.put("zd", "吉大");
        map2.put("lx", "充值");
        map2.put("dz", "吉大区海滨南路");
        list.add(map2);

        Map<String,String> map3=new HashMap<String, String>();
        map3.put("zd", "横琴");
        map3.put("lx", "充值");
        map3.put("dz", "圣方济各堂区琴海东路");
        list.add(map3);

        Map<String,String> map4=new HashMap<String, String>();
        map4.put("zd", "拱北");
        map4.put("lx", "售卡，充值");
        map4.put("dz", "望德堂区昌盛路");
        list.add(map4);

        Map<String,String> map5=new HashMap<String, String>();
        map5.put("zd", "斗门");
        map5.put("lx", "充值");
        map5.put("dz", "斗门区和风中街");
        list.add(map5);
        //                                    （主页面，数据，样式页面，数据的Key,数据放置的位置）；key和数据位置要对齐
        simpleAdapter=new SimpleAdapter(this,list,R.layout.listview,form,to);
        listView1.setAdapter(simpleAdapter);
    }
    /*
    选择为空的页面显示函数
     */
    private void kong(){
        if(list.size()==0) {
            listView1.setBackgroundResource(R.mipmap.kong2);
            Toast.makeText(this, "您选择了：" + tv1.getText() + "," + tv2.getText() + "," + tv3.getText() + "为空，请重新选择", LENGTH_SHORT).show();
        }
    }
    /*
    点击选择项目Listview的内容切换的函数
     */
    public void dianjiqiehuang(Button bt){
        listView1.setBackgroundResource(0);//去掉背景图片
        if(zhandian!=null && yewu==null) {
            list.clear();
            list();
            for (int i = 0; i < list.size(); i++) {
                if (bt.getText().equals(list.get(i).get("zd"))) {
                    list1.add(list.get(i));//将符合要求的Map添加到list1中
                }
            }
            list.clear();//将list中所有数据清空
            for (int i = 0; i < list1.size(); i++) {
                list.add(list1.get(i));//把符合要求的Map重新添加给list，达到Listview中只显示符合要求的数据
            }
            list1.clear();//释放所有内存
            simpleAdapter.notifyDataSetChanged();//更新listview视图
            zhandian = (String) bt.getText();
        }else if (zhandian==null && yewu==null){
            for (int i = 0; i < list.size(); i++) {
                if (bt.getText().equals(list.get(i).get("zd"))) {
                    list1.add(list.get(i));//将符合要求的Map添加到list1中
                }
            }
            list.clear();//将list中所有数据清空
            for (int i = 0; i < list1.size(); i++) {
                list.add(list1.get(i));//把符合要求的Map重新添加给list，达到Listview中只显示符合要求的数据
            }
            list1.clear();//释放所有内存
            simpleAdapter.notifyDataSetChanged();//更新listview视图
            zhandian = (String) bt.getText();
        }else if (zhandian==null && yewu!=null){
            for (int i = 0; i < list.size(); i++) {
                if (bt.getText().equals(list.get(i).get("zd"))) {
                    list1.add(list.get(i));//将符合要求的Map添加到list1中
                }
            }
            list.clear();//将list中所有数据清空
            for (int i = 0; i < list1.size(); i++) {
                list.add(list1.get(i));//把符合要求的Map重新添加给list，达到Listview中只显示符合要求的数据
            }
            list1.clear();//释放所有内存
            simpleAdapter.notifyDataSetChanged();//更新listview视图
            zhandian = (String) bt.getText();
        }else if(zhandian!=null && yewu!=null){
            list.clear();
            list();
            for (int i = 0; i < list.size(); i++) {
                if (bt.getText().equals(list.get(i).get("zd"))) {
                    list1.add(list.get(i));//将符合要求的Map添加到list1中
                }
            }
            list.clear();//将list中所有数据清空
            for (int i = 0; i < list1.size(); i++) {
                list.add(list1.get(i));//把符合要求的Map重新添加给list，达到Listview中只显示符合要求的数据
            }
            list1.clear();//释放所有内存
            simpleAdapter.notifyDataSetChanged();//更新listview视图
            zhandian = (String) bt.getText();
            yewuxuanze();
        }
    }
    /*
    业务类型选择的监控
     */
    public void yewuxuanze() {
        listView1.setBackgroundResource(0);//设置listview背景为空
        if (yewu != null && zhandian==null) {
            list.clear();
            list();
            ArrayList<Map<String, String>> list3 = new ArrayList<Map<String, String>>();
            for (int i = 0; i < list.size(); i++) {
                if (list.get(i).get("lx").length() > 2) {
                    list3.add(list.get(i));
                } else {
                    if (yewu.equals(list.get(i).get("lx"))) {
                        list3.add(list.get(i));
                    }
                }
            }
            list.clear();
            for (int j = 0; j < list3.size(); j++) {
                list.add(list3.get(j));
            }
            list3.clear();
            simpleAdapter.notifyDataSetChanged();
        }else if(yewu!=null && zhandian!=null){//当地区有选择时在进行业务类型选择，先把listview重置在进行地区选择之后进行业务类型选择
            list.clear();                         //如果直接在当前list view中选择的话第二次选不同业务类型时会有错误。
            list();
            ArrayList<Map<String, String>> list4 = new ArrayList<Map<String, String>>();
            for (int i = 0; i < list.size(); i++) {
                if (zhandian.equals(list.get(i).get("zd"))) {
                    list1.add(list.get(i));//将符合要求的Map添加到list1中
                }
            }
            for(int k=0;k<list1.size();k++){
                if (list1.get(k).get("lx").length()>2){
                    list4.add(list1.get(k));
                }else {
                    if(yewu.equals(list1.get(k).get("lx"))){
                        list4.add(list1.get(k));
                    }
                }
            }
            list1.clear();
            list.clear();
            for (int l=0;l<list4.size();l++){
                list.add(list4.get(l));
            }
            list4.clear();
            simpleAdapter.notifyDataSetChanged();
        }
    }
}
